package com.woniuxy.processservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.DateUtil;
import com.woniuxy.processclient.client.OrderFeignClient;
import com.woniuxy.processclient.client.ProductFeignClient;
import com.woniuxy.processclient.client.dto.AloneCourseProductDto;
import com.woniuxy.processclient.client.dto.OrderDto;
import com.woniuxy.processdao.mapper.PricardMapper;
import com.woniuxy.processdao.model.Pricard;
import com.woniuxy.processmodel.dto.PriCardDto;
import com.woniuxy.processmodel.form.UpdatePricardForm;
import com.woniuxy.processservice.exception.ProcessException;
import com.woniuxy.processservice.service.PricardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 私教课流程表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class PricardServiceImpl extends ServiceImpl<PricardMapper, Pricard> implements PricardService {

    @Autowired
    private PricardMapper pricardMapper;
    @Resource
    private OrderFeignClient orderFeignClient;
    @Resource
    private ProductFeignClient productFeignClient;
    @Override
    public void addValidCardStep(String orderNum) {
        QueryWrapper<Pricard> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", orderNum);
        Pricard pricard = pricardMapper.selectOne(wrapper);
        if(!BeanUtil.isEmpty(pricard)){
            throw new ProcessException(60001,"该私教课订单进度已存在");
        }
        OrderDto orderDto = orderFeignClient.findByOrderNum(orderNum).getData();
        if(BeanUtil.isEmpty(orderDto)){
            throw new ProcessException(60009,"该私教课订单不存在");
        }
        AloneCourseProductDto productDto = productFeignClient.findAloneCourseProductById(orderDto.getProductId()).getData();
        Pricard pricard1 = new Pricard();
        pricard1.setOrderNum(orderNum);
        pricard1.setCoachId(orderDto.getCoachId());
        pricard1.setPricardCount(productDto.getCoachProductCount());
        pricard1.setPricardUse(0);
        pricard1.setUserId(orderDto.getUserId());
        pricard1.setVenueId(orderDto.getVenueId());
        pricardMapper.insert(pricard1);
    }

    @Override
    public void updatePricard(UpdatePricardForm form, Integer coachId) {
        QueryWrapper<Pricard> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",form.getOrderNum());
        Pricard pricard = pricardMapper.selectOne(wrapper);
        if(BeanUtil.isEmpty(pricard)){
            throw new ProcessException(60002,"该私教课订单进度不存在");
        }
        if(pricard.getCoachId()!=coachId){
            throw new ProcessException(60003,"该私教订单进度教练异常");
        }
        if(pricard.getPricardCount()==pricard.getPricardUse()){
            throw new ProcessException(60004,"该私教订单进度已完成");
        }
        Result<OrderDto> result = orderFeignClient.findByOrderNum(pricard.getOrderNum());
        OrderDto data = result.getData();
        if(data.getOrderState()!=1){
            throw new ProcessException(60005,"该私教订单状态异常");
        }
        pricard.setPricardUse(pricard.getPricardUse()+1);
        pricardMapper.updateById(pricard);
        if(pricard.getPricardUse()==pricard.getPricardCount()){
            pricard.setCompleteTime(DateUtil.dateTimeToString(new Date()));
            pricardMapper.updateById(pricard);
            //将订单状态更改为已完成
            orderFeignClient.updateOrderStateByOrderNum(form.getOrderNum(),3);
        }
    }

    @Override
    public Pricard lookPricard(Integer pricardId) {
        Pricard pricard = pricardMapper.selectById(pricardId);
        return pricard;
    }

    @Override
    public List<PriCardDto> userLookPricard(String orderNum, Integer userId) {
        QueryWrapper<Pricard> wrapper = new QueryWrapper<>();
        wrapper.like(!BeanUtil.isEmpty(orderNum),"order_num",orderNum);
        wrapper.eq("user_id",userId);
        List<Pricard> pricards = pricardMapper.selectList(wrapper);
        List<PriCardDto> dtos = BeanUtil.copyToList(pricards, PriCardDto.class);
        return dtos;
    }

    @Override
    public List<PriCardDto> coachLookPricard(String orderNum, Integer coachId) {
        QueryWrapper<Pricard> wrapper = new QueryWrapper<>();
        wrapper.like(!BeanUtil.isEmpty(orderNum),"order_num",orderNum);
        wrapper.eq("coach_id",coachId);
        List<Pricard> pricards = pricardMapper.selectList(wrapper);
        List<PriCardDto> dtos = BeanUtil.copyToList(pricards, PriCardDto.class);
        return dtos;
    }
}
